package com.minimalist.common.feign.fallback.lindi;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.contracttemplate.LindiContractTemplateVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationcontract.LindiTradeApplicationContractQueryVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationcontract.LindiTradeApplicationContractVO;
import com.minimalist.common.feign.client.lindi.LindiTradeApplicationContractFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import com.minimalist.common.response.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 林地交易申请合同 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class LindiTradeApplicationContractFeignClientFallbackFactory extends BaseFallbackFactory<LindiTradeApplicationContractFeignClient>{
    private static final Logger log = LoggerFactory.getLogger(LindiTradeApplicationContractFeignClientFallbackFactory.class);


    @Override
    public LindiTradeApplicationContractFeignClient create(Throwable cause){
        return new LindiTradeApplicationContractFeignClient(){
            @Override
            public R<?> addTradeApplicationContract(LindiTradeApplicationContractVO tradeApplicationContractVO){
                logServiceUnavailable("添加交易申请合同", tradeApplicationContractVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "添加交易申请合同失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> deleteTradeApplicationContractByTradeApplicationContractId(Long tradeApplicationContractId){
                logServiceUnavailable("删除交易申请合同", tradeApplicationContractId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "删除交易申请合同失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<?> updateTradeApplicationContractByTradeApplicationContractId(LindiTradeApplicationContractVO tradeApplicationContractVO){
                logServiceUnavailable("修改交易申请合同", tradeApplicationContractVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "修改交易申请合同失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<PageResp<LindiTradeApplicationContractVO>> getPageTradeApplicationContractList(LindiTradeApplicationContractQueryVO queryVO){
                logServiceUnavailable("分页查询交易申请合同列表", queryVO, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "分页查询交易申请合同列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiTradeApplicationContractVO> getTradeApplicationContractByTradeApplicationContractId(Long tradeApplicationContractId){
                logServiceUnavailable("获取交易申请合同详情", tradeApplicationContractId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "获取交易申请合同详情失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiTradeApplicationContractVO> getTradeApplicationContractByTradeApplicationId(Long tradeApplicationId){
                logServiceUnavailable("根据交易申请ID查询合同信息", tradeApplicationId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "根据交易申请ID查询合同信息失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<LindiTradeApplicationContractVO>> getTradeApplicationContractListByTradeApplicationId(Long tradeApplicationId){
                logServiceUnavailable("根据交易申请ID查询交易申请合同列表", tradeApplicationId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "根据交易申请ID查询交易申请合同列表失败，服务不可用: " + cause.getMessage());
            }

            @Override
            public R<List<LindiContractTemplateVO>> getContractTemplateList(){
                logServiceUnavailable("获取合同模板列表", cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(), "服务不可用: " + cause.getMessage());
            }

            @Override
            public R<LindiTradeApplicationContractVO> generateContract(
                    Long tradeApplicationId, Long contractTemplateId
                    , Long createId, Long tenantId){
                logServiceUnavailable("生成合同", "tradeApplicationId: " + tradeApplicationId + ", contractTemplateId: " + contractTemplateId, cause);
                return R.fail(HttpStatus.SERVICE_UNAVAILABLE.value(),
                        "生成合同失败，服务不可用: " + cause.getMessage());
            }
        };
    }
}
